package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Machine;
import com.tbit.uqbike.object.pojo.ParkMachine;
import com.tbit.uqbike.object.pojo.ParkPoint;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.base.MachineService;
import com.tbit.uqbike.service.base.ParkPointService;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.ParkMachineService;
import com.tbit.uqbike.webmanager.dao.core.ParkMachineDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

@Slf4j
@Service("parkMachineService")
public class ParkMachineServiceImpl implements ParkMachineService {
    @Autowired
    private ParkMachineDao parkMachineDao;
    @Autowired
    private MachineService machineService;
    @Autowired
    private ParkPointService parkPointService;
    @Autowired
    private AccountService accountService;
    @Override
    public void insert(ParkMachine parkMachine) {
        parkMachineDao.insert(parkMachine);
    }

    @Override
    public void del(Integer machineId) {
        parkMachineDao.del(machineId);
    }

    @Override
    public List<ParkMachine> getByParkPointId(Integer parkPointId) {
        return parkMachineDao.getByParkPointId(parkPointId);
    }

    @Override
    public ParkMachine getByMachineId(Integer machineId) {
        return parkMachineDao.getByMachineId(machineId);
    }

    @Override
    //@Transactional
    public Map<Integer, List<String>> deliverBatch(LoginInfo loginInfo, Integer parkPointId, List<String> machineNOs) {
        Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
        /**不存在的设备编号*/
        List<String> machineNotExist = new ArrayList<String>();
        /**无权的设备编号*/
        List<String> machineNotauth = new ArrayList<String>();
        /**查询设备信息*/
        List<Machine> machines = machineService.getByMachineNOs(machineNOs);
        Map<String, Integer> existMap = new HashMap<>();
        for (Machine machine : machines) {
            existMap.put(machine.getMachineNO(), machine.getMachineId());
        }
        /**判断不存在的设备编号*/
        for (String machineNO : machineNOs) {
            if (!existMap.containsKey(machineNO)) {
                machineNotExist.add(machineNO);
            }
        }
        if (machineNotExist.size() > 0) {
            map.put(1, machineNotExist);
        }
        List<Integer> accountIds = accountService.getAllOperateAccountId(loginInfo);
        //权限判断
        ParkPoint parkPoint = parkPointService.getByParkPointId(parkPointId);
        if (parkPoint == null) {
            throw new BaseException("停车点不存在");
        }
        if (!accountIds.contains(parkPoint.getAccountId())) {
            //停车点和账户权限判断
            throw new BaseException("");
        }
        for (Machine machine : machines) {
            if (!machine.getMachineId().equals(parkPoint.getAccountId())) {
                //设备和停车点所属权限判断
                machineNotauth.add(machine.getMachineNO());
                existMap.remove(machine.getMachineNO());
            } else {
                if (!accountIds.equals(machine.getAccountId())) {
                    //设备和账户权限判断
                    machineNotauth.add(machine.getMachineNO());
                    existMap.remove(machine.getMachineNO());
                }
            }
        }
        if (machineNotauth.size() > 0) {
            map.put(2, machineNotauth);
        }
        if (machines.size() > 0) {
            /**考虑到批量投放的数据量不会太大，所以没有做批量的操作，后期如果数据量太大需要优化*/
            for (String key : existMap.keySet()) {
                /**删除已经存在的投放记录*/
                del(existMap.get(key));
                /**添加新投放的记录*/
                ParkMachine parkMachine = new ParkMachine();
                parkMachine.setAddtime(new Date());
                parkMachine.setMachineid(existMap.get(key));
                parkMachine.setParkpointid(parkPointId);

                insert(parkMachine);
            }
        }
        return map;
    }


    /**
     * 根据停车点id批量删除
     * @param parkPointIds parkPointIds
     */
    @Override
    public void deleteByParkPointIds(List<Integer> parkPointIds) {
        if (CollectionUtil.isNotEmpty(parkPointIds)) {
            parkMachineDao.deleteByParkPointIds(parkPointIds);
        }
    }

}  